package com.woniu.tms.manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.woniu.tms.entity.*;
import com.woniu.tms.login.MD5Utils;
import com.woniu.tms.manage.service.UserService;
import com.woniu.tms.manage.utils.MyUtils;
import com.woniu.tms.mapper.AdminMapper;
import com.woniu.tms.mapper.RoleMapper;
import com.woniu.tms.mapper.UserMapper;
import com.woniu.tms.mapper.UserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    RoleMapper roleMapper;

    @Override
    public List<User> getByCondition(String condition, String rname, Integer pageNum, Integer pageSize) {
        UserExample userExample = new UserExample();
        List<Integer> list = null;
        if (!"角色".equals(rname)) {
            RoleExample roleExample = new RoleExample();
            roleExample.createCriteria().andRnameEqualTo(rname);
            List<Role> roleList = roleMapper.selectByExample(roleExample);
            // 查出rid对应所有角色的uid
            UserRoleExample userRoleExample = new UserRoleExample();
            userRoleExample.createCriteria().andRidEqualTo(roleList.get(0).getRid());
            List<UserRole> userRoleList = userRoleMapper.selectByExample(userRoleExample);
            if (userRoleList.size() != 0) {
                list = new ArrayList<>();
                for (UserRole userRole : userRoleList) {
                    list.add(userRole.getUid());
                }
                UserExample.Criteria criteria1 = userExample.createCriteria();
                criteria1.andUidIn(list).andUnameLike("%" + condition + "%");
                UserExample.Criteria criteria2 = userExample.createCriteria();
                criteria2.andUidIn(list).andUeamailLike("%" + condition + "%");
                UserExample.Criteria criteria3 = userExample.createCriteria();
                criteria3.andUidIn(list).andUstatusLike("%" + condition + "%");
                UserExample.Criteria criteria4 = userExample.createCriteria();
                criteria3.andUidIn(list).andSnoLike("%" + condition + "%");
                userExample.or(criteria2);
                userExample.or(criteria3);
                userExample.or(criteria4);
            } else {
                return new ArrayList<User>();
            }
        } else {
            userExample.or().andUnameLike("%" + condition + "%");
            userExample.or().andUeamailLike("%" + condition + "%");
            userExample.or().andUstatusLike("%" + condition + "%");
            userExample.or().andSnoLike("%" + condition + "%");
        }
        userExample.setOrderByClause("uid desc");
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.selectByExample(userExample);
    }

    @Override
    public void update(User user) {
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void add(User user, Integer rid) {
        // 判断角色 加不同的前缀
        String suffix = "";
        if (rid == 1) {
            suffix = "WN";
        } else if (rid == 2 || rid == 3 || rid == 4 || rid == 6) {
            suffix = "TH";
        } else if (rid == 5) {
            suffix = "CI";
        } else {
            suffix = "AD";
        }
        String sno = MyUtils.getSno(suffix);
        user.setSno(sno);
        // 密码加密
        user.setUpwd(MD5Utils.generate(user.getUpwd()));
        userMapper.insert(user);
        // 根据sno找出user对象
        UserExample userExample = new UserExample();
        userExample.createCriteria().andSnoEqualTo(sno);
        List<User> users = userMapper.selectByExample(userExample);
        Integer uid = users.get(0).getUid();
        UserRole userRole = new UserRole();
        userRole.setUid(uid);
        userRole.setRid(rid);
        userRoleMapper.insert(userRole);
    }

    @Override
    public void delete(Integer uid) {
        userMapper.deleteByPrimaryKey(uid);
    }

    @Override
    public void deleteAll(Integer[] uids) {
        for (Integer uid : uids) {
            userMapper.deleteByPrimaryKey(uid);
        }
    }
}
